package com.zzx.controller;

import com.zzx.domain.Vo.AttributeVo;
import com.zzx.service.AttributeService;
import com.zzx.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/categories")
public class AttributeController {

    @Autowired
    private AttributeService attributeService;

    /**
     * 通过id查询参数列表
     * @param id
     * @param sel
     * @return
     */
    @PreAuthorize("hasAuthority('分类参数')")
    @GetMapping("/{id}/attributes")
    public ResponseResult ParamsList(@PathVariable Integer id, String sel ){
        return attributeService.ParamsList(id,sel);
    }

    /**
     * 添加动态参数或者静态属性
     * @return
     */
    @PreAuthorize("hasAuthority('分类参数添加')")
    @PostMapping("/{id}/attributes")
    public ResponseResult AddParams(@PathVariable("id")Integer id, @RequestBody AttributeVo attributeVo){
        return attributeService.AddParams(id,attributeVo);
    }

    /**
     * 通过id删除指定分类的下的参数
     * @param id
     * @param attrid
     * @return
     */
    @PreAuthorize("hasAuthority('分类参数删除')")
    @DeleteMapping("/{id}/attributes/{attrid}")
    public ResponseResult DeleteParam(@PathVariable("id")Integer id,@PathVariable("attrid") Integer attrid){
        return attributeService.DeleteParam(id,attrid);
    }

    /**
     *  根据id查询参数
     * @param id
     * @param attrId
     * @param attr_sel
     * @param attr_vals
     * @return
     */
    @PreAuthorize("hasAuthority('分类参数详情')")
    @GetMapping("/{id}/attributes/{attrId}")
    public ResponseResult selectParamsById(@PathVariable("id")Integer id,@PathVariable("attrId")Integer attrId,String attr_sel,@RequestParam(required = false)String attr_vals){
        return attributeService.selectParamsById(id,attrId,attr_sel,attr_vals);
    }

    /**
     * 更新参数信息
     * @param id
     * @param attrId
     * @param attributeVo
     * @return
     */
    @PreAuthorize("hasAuthority('分类参数添加')")
    @PutMapping("/{id}/attributes/{attrId}")
    public ResponseResult UpdateParam(@PathVariable("id")Integer id,@PathVariable("attrId") Integer attrId,@RequestBody AttributeVo attributeVo){
        return attributeService.UpdateParam(id,attrId,attributeVo);
    }
}
